import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_bool_entity.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';

toChangePassword() {
  Get.toNamed(LFRoute.pathChangePassword);
}

class LfChangePasswordLogic extends GetxController {
  LfAuthService authService = Get.find();
  RxString oldPassword = ''.obs;
  RxString newPassword = ''.obs;
  RxString confirmPassword = ''.obs;
  FocusNode oldPasswordNode = FocusNode();
  FocusNode newPasswordNode = FocusNode();
  FocusNode confirmPasswordNode = FocusNode();
  TextEditingController oldPasswordController = TextEditingController();
  TextEditingController newPasswordController = TextEditingController();
  TextEditingController confirmPasswordController = TextEditingController();
  bool get passwordMatched =>
      (newPassword.value == confirmPassword.value) &&
      (newPassword.value.isNotEmpty && confirmPassword.value.isNotEmpty);
  bool get enableContinue => passwordMatched && newPassword.value.length >= 6;
  changePassword() async {
    if (!passwordMatched) {
      return;
    }
    showLoading();
    await LfDio().post<LfBoolEntity>(url: apiChangePassword, params: {
      "oldPW": oldPassword.value.trim(),
      "newPW": newPassword.value.trim()
    }).then((value) {
      hideLoading();
      showActionTips(
        tips: strPasswordUpdateSuccessfully.tr,
        showCancel: false,
        dismissible: false,
        actionButtonTip: strOK.tr,
        action: () {
          authService.loginOut();
        },
      );
    }, onError: (error) {
      hideLoading();
      if (error is NetError) {
        showSnackBar(error.msg);
      }
    });
  }
}
